<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Comparison with C, R, FORTRAN-style Free Functions</title>
<link rel="stylesheet" href="../../../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../index.html" title="Math Toolkit 4.2.1">
<link rel="up" href="../weg.html" title="Worked Examples">
<link rel="prev" href="find_eg/find_mean_and_sd_eg.html" title="Find mean and standard deviation example">
<link rel="next" href="c_sharp.html" title="Using the Distributions from Within C#">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="find_eg/find_mean_and_sd_eg.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../weg.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="c_sharp.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="math_toolkit.stat_tut.weg.nag_library"></a><a class="link" href="nag_library.html" title="Comparison with C, R, FORTRAN-style Free Functions">Comparison with
        C, R, FORTRAN-style Free Functions</a>
</h4></div></div></div>
<p>
          You are probably familiar with a statistics library that has free functions,
          for example the classic <a href="http://nag.com/numeric/CL/CLdescription.asp" target="_top">NAG
          C library</a> and matching <a href="http://nag.com/numeric/FL/FLdescription.asp" target="_top">NAG
          FORTRAN Library</a>, <a href="http://office.microsoft.com/en-us/excel/HP052090051033.aspx" target="_top">Microsoft
          Excel BINOMDIST(number_s,trials,probability_s,cumulative)</a>, <a href="http://www.r-project.org/" target="_top">R</a>, <a href="http://www.ptc.com/products/mathcad/mathcad14/mathcad_func_chart.htm" target="_top">MathCAD
          pbinom</a> and many others.
        </p>
<p>
          If so, you may find 'Distributions as Objects' unfamiliar, if not alien.
        </p>
<p>
          However, <span class="bold"><strong>do not panic</strong></span>, both definition
          and usage are not really very different.
        </p>
<p>
          A very simple example of generating the same values as the <a href="http://nag.com/numeric/CL/CLdescription.asp" target="_top">NAG
          C library</a> for the binomial distribution follows. (If you find slightly
          different values, the Boost C++ version, using double or better, is very
          likely to be the more accurate. Of course, accuracy is not usually a concern
          for most applications of this function).
        </p>
<p>
          The <a href="http://www.nag.co.uk/numeric/cl/manual/pdf/G01/g01bjc.pdf" target="_top">NAG
          function specification</a> is
        </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">nag_binomial_dist</span><span class="special">(</span><span class="identifier">Integer</span> <span class="identifier">n</span><span class="special">,</span> <span class="keyword">double</span> <span class="identifier">p</span><span class="special">,</span> <span class="identifier">Integer</span> <span class="identifier">k</span><span class="special">,</span>
<span class="keyword">double</span> <span class="special">*</span><span class="identifier">plek</span><span class="special">,</span> <span class="keyword">double</span> <span class="special">*</span><span class="identifier">pgtk</span><span class="special">,</span> <span class="keyword">double</span> <span class="special">*</span><span class="identifier">peqk</span><span class="special">,</span> <span class="identifier">NagError</span> <span class="special">*</span><span class="identifier">fail</span><span class="special">)</span>
</pre>
<p>
          and is called
        </p>
<pre class="programlisting"><span class="identifier">g01bjc</span><span class="special">(</span><span class="identifier">n</span><span class="special">,</span> <span class="identifier">p</span><span class="special">,</span> <span class="identifier">k</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">plek</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">pgtk</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">peqk</span><span class="special">,</span> <span class="identifier">NAGERR_DEFAULT</span><span class="special">);</span>
</pre>
<p>
          The equivalent using this Boost C++ library is:
        </p>
<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">;</span>  <span class="comment">// Using declaration avoids very long names.</span>
<span class="identifier">binomial</span> <span class="identifier">my_dist</span><span class="special">(</span><span class="number">4</span><span class="special">,</span> <span class="number">0.5</span><span class="special">);</span> <span class="comment">// c.f. NAG n = 4, p = 0.5</span>
</pre>
<p>
          and values can be output thus:
        </p>
<pre class="programlisting"><span class="identifier">cout</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">my_dist</span><span class="special">.</span><span class="identifier">trials</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span>             <span class="comment">// Echo the NAG input n = 4 trials.</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">my_dist</span><span class="special">.</span><span class="identifier">success_fraction</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span>   <span class="comment">// Echo the NAG input p = 0.5</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">my_dist</span><span class="special">,</span> <span class="number">2</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">"  "</span>             <span class="comment">// NAG plek with k = 2</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">complement</span><span class="special">(</span><span class="identifier">my_dist</span><span class="special">,</span> <span class="number">2</span><span class="special">))</span> <span class="special">&lt;&lt;</span> <span class="string">"  "</span> <span class="comment">// NAG pgtk with k = 2</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">pdf</span><span class="special">(</span><span class="identifier">my_dist</span><span class="special">,</span> <span class="number">2</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>            <span class="comment">// NAG peqk with k = 2</span>
</pre>
<p>
          <code class="computeroutput"><span class="identifier">cdf</span><span class="special">(</span><span class="identifier">dist</span><span class="special">,</span> <span class="identifier">k</span><span class="special">)</span></code> is
          equivalent to NAG library <code class="computeroutput"><span class="identifier">plek</span></code>,
          lower tail probability of &lt;= k
        </p>
<p>
          <code class="computeroutput"><span class="identifier">cdf</span><span class="special">(</span><span class="identifier">complement</span><span class="special">(</span><span class="identifier">dist</span><span class="special">,</span> <span class="identifier">k</span><span class="special">))</span></code> is
          equivalent to NAG library <code class="computeroutput"><span class="identifier">pgtk</span></code>,
          upper tail probability of &gt; k
        </p>
<p>
          <code class="computeroutput"><span class="identifier">pdf</span><span class="special">(</span><span class="identifier">dist</span><span class="special">,</span> <span class="identifier">k</span><span class="special">)</span></code> is
          equivalent to NAG library <code class="computeroutput"><span class="identifier">peqk</span></code>,
          point probability of == k
        </p>
<p>
          See <a href="../../../../../example/binomial_example_nag.cpp" target="_top">binomial_example_nag.cpp</a>
          for details.
        </p>
</div>
<div class="copyright-footer">Copyright © 2006-2021 Nikhar Agrawal, Anton Bikineev, Matthew Borland,
      Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno
      Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde,
      Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle
      Walker and Xiaogang Zhang<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="find_eg/find_mean_and_sd_eg.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../weg.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="c_sharp.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
